<template>
  <div class="page-login">
    <!-- logo -->
    <!--<img class="page-login&#45;&#45;logo" src="./image/logo@2x.png">-->
    <!-- form -->
    <div class="page-login--form">
      <img src="image/login/logo.png" style="position: absolute;left: 50%;transform: translateX(-50%);width: 100px;top: -120px;">
      <div style="padding-top: calc(40px + 30px);">
        <div class="login-type">
          <div style="title-text">用户登录</div>
        </div>
        <el-form
          ref="loginForm"
          label-position="top"
          :rules="rules"
          :model="formLogin"
          size="default">
          <el-form-item prop="username">
            <el-input
              type="text"
              autofocus
              v-model="formLogin.username"
              placeholder="用户名" prefix-icon="el-icon-user-solid"
              @keyup.enter.native="submit">
            </el-input>
          </el-form-item>
          <el-form-item prop="password" style="position: relative;">
            <el-input
              type="password"
              v-model="formLogin.password"
              placeholder="密码" prefix-icon="el-icon-unlock"
              @keyup.enter.native="submit">
            </el-input>
            <span v-if="errMsg" style="color: red;font-size: 12px;float: right;position: absolute;right: 5px;top: 40px">{{errMsg}}</span>
          </el-form-item>
          <el-form-item v-if="false" prop="code">
            <el-input
              type="text"
              v-model="formLogin.code"
              placeholder="验证码">
              <template slot="append">
                <img class="login-code" src="image/login-code.png">
              </template>
            </el-input>
          </el-form-item>
          <el-button size="default" @click="submit" style="background: #F29901;color: #fff"
                     class="button-login">
            登&#12288;录
          </el-button>
          <div class="page-login--options" flex="main:justify cross:center">
            <span @click="remember=!remember" style="cursor: pointer"><el-checkbox v-model="remember" style="color:#409EFF;padding-right: 5px"/>记住密码</span>
            <span style="cursor: pointer"><d2-icon name="question-circle" style="font-size: 16px"/> 忘记密码</span>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
  import moment from 'moment'
  import {mapActions} from 'vuex';
  import util from '@/assets/utils/zhzf-util';
  import cache from '@/assets/utils/data-cache';
  import {initSysMenus} from "@/assets/utils/menu-util";

  export default {
    data() {
      return {
        remember: false,
        errMsg: '',
        // 表单
        formLogin: {},
        // 表单校验
        rules: {
          username: [
            {
              required: true,
              message: '请输入用户名',
              trigger: 'blur'
            }
          ],
          password: [
            {
              required: true,
              message: '请输入密码',
              trigger: 'blur'
            }
          ],
          code: [
            {
              required: true,
              message: '请输入验证码',
              trigger: 'blur'
            }
          ]
        }
      }
    },
    methods: {
      ...mapActions('d2admin/account', [
        'login'
      ]),
      refreshTime() {
        this.time = moment().format('HH:mm:ss')
      },
      /**
       * @description 提交表单
       */
      // 提交登录信息
      submit() {
        cache.clear();
        this.errMsg = '';
        this.$refs.loginForm.validate(async (valid) => {
          util.showLoading();
          if (!valid) {
            util.hideLoading();
            // 登录表单校验失败
            console.warn('表单校验失败，请检查');
            return;
          }
          try {
            //登录
            await this.login({username: this.formLogin.username, password: this.formLogin.password});
            //初始化菜单
            await initSysMenus();
            //跳转主页
            this.$router.replace(this.$route.query.redirect || '/');
            util.hideLoading();
          } catch (err) {
            util.hideLoading();
            this.errMsg = err.msg;
            console.warn(err)
          }
        })
      }
    }
  }
</script>

<style lang="scss">
  .page-login {
    width: 100%;
    height: 100%;
    background: url("/zhzf/image/login/bg.jpg");
    background-position: center;
    background-repeat: no-repeat;
    background-size: 100% 100%;
    position: relative;
    // 登录表单
    .page-login--form {
      width: calc(684px * 0.7);
      height: calc(518px * 0.8);
      background: url("/zhzf/image/login/login_bg.png");
      background-position: center;
      background-repeat: no-repeat;
      background-size: 100% 100%;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);

      .login-type {
        padding-left: 30px;
        line-height: 45px;
        font-weight: bold;
        margin-top: -20px;
        border-bottom: 1px solid #ccc;
        font-size: 18px;
      }

      .el-form {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        width: 80%;
        padding-top: 100px;

        .el-form-item {
          margin-bottom: 35px;
        }

        .el-input {
          height: 45px;

          .el-input__icon {
            line-height: 43px;
            padding-left: 5px;
          }

          .el-input__inner {
            padding-left: 45px;
          }
        }

        .el-button:hover {
          opacity: 0.8;
        }


        // 登陆选项
        .page-login--options {
          padding: 15px 5px 0 5px;
          margin: 0;
          font-size: 12px;
          color: $color-primary;
          margin-bottom: 15px;
          font-weight: bold;
        }
      }

      // 登录按钮
      .button-login {
        width: 100%;
      }

      // 输入框左边的图表区域缩窄
      .el-input-group__prepend {
        padding: 0px 14px;
      }

      .login-code {
        height: 40px - 2px;
        display: block;
        margin: 0px -20px;
        border-top-right-radius: 2px;
        border-bottom-right-radius: 2px;
      }
    }

  }
</style>
